export class DateUtil {
    /**
     *  格式日期
     * @param date
     * @returns Date
     */
    static parseDate(date) {
        if (!date) {
            return date;
        }
        if (date instanceof Date) {
            return date;
        } else if (typeof date === 'number') {
            return new Date(date * 1000);
        } else if (typeof date === 'string') {
            return new Date(date);
        }
        throw new Error(`无效的数据:"${date}",转换为日期格式失败`);
    }

    static format(date, fmt = 'yyyy-MM-dd hh:mm:ss') { //
        date = this.parseDate(date);
        if (!date) {
            return null;
        }
        const o = {
            'M+': date.getMonth() + 1, // Month
            'd+': date.getDate(), // Day
            'h+': date.getHours(), // Hour
            'm+': date.getMinutes(), // Minute
            's+': date.getSeconds(), // Second
            'q+': Math.floor((date.getMonth() + 3) / 3), // Season
            'S': date.getMilliseconds() // millesecond
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() +

                '').substr(4 - RegExp.$1.length));
        }
        for (const k in o) {
            if (new RegExp('(' + k + ')').test(fmt)) {
                fmt = fmt.replace(RegExp.$1,
                    (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
            }
        }
        return fmt;
    }

    /**
     *
     * @param fmt
     * @returns {string | *}
     */
    static now(fmt = 'yyyy-MM-dd hh:mm:ss') {
        return this.format(new Date(), fmt);
    }

    static offset({
        dateTime = new Date(),
        days = 0,
        hours = 0,
        mintue = 0,
        fmt = 'yyyy-MM-dd hh:mm:ss',
    }) {
        const date = dateTime;
        const dt = days * 24 * 60 * 60 * 1000;
        const ht = hours * 60 * 60 * 1000;
        const mt = mintue * 60 * 1000;
        date.setTime(date.getTime() + dt + ht + mt);
        return this.format(date, fmt);
    }

    static friendlyDate(dateTime, timeOutMessage = '') {
        if (!dateTime) {
            return dateTime;
        }
        let now = Date.parse(String(new Date())) / 1000;
        let time = this.parseDate(dateTime);
        let limit = now - time.getTime() / 1000;
        let content = '';
        let isfuture = false;
        if (limit < 0) {
            isfuture = true;
            limit = Math.abs(limit);
        }
        if (limit < 3600) {
            const limitTime = Math.floor(limit / 60);
            content = !isfuture ? `${timeOutMessage}${limitTime}分钟` : limitTime + '分钟后';
        } else if (limit >= 3600 && limit < 86400) {
            const limitTime = Math.floor(limit / 3600);
            content = !isfuture ? `${timeOutMessage}${limitTime}小时` : limitTime + '小时后';
        } else if (limit >= 86400 && limit < 2592000) {
            const limitTime = Math.floor(limit / 86400);
            content = !isfuture ? `${timeOutMessage}${limitTime}天` : limitTime + '天后';
        } else if (limit >= 2592000 && limit <= 31536000) {
            const limitTime = Math.floor(limit / 2592000);
            content = !isfuture ? `${timeOutMessage}${limitTime}个月` : limitTime + '个月后';
        } else {
            if (isfuture) {
                return '很久后';
            }
            return timeOutMessage ? `很久前${timeOutMessage}` : '很久前';
        }
        return {
            content,
            isfuture
        };
    }

    static leftDays(datetime) {
        if (!datetime) {
            return null;
        }
        const diffTime = this.parseDate(datetime)
            .getTime() - new Date().getTime();
        return Math.floor(diffTime / (1000 * 60 * 60 * 24));
    }
}
